﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileApplication.UNEF
{
    public partial class ListForm : Form
    {
        private DataTable dataTable = new DataTable();

        public ListForm()
        {
            
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Age");
            dataTable.Columns.Add("Province");
            dataTable.Columns.Add("CurTime");
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            DataGridViewColumnCollection columns = this.dataGridView1.Columns;
            for (int i = 0; i < 3; i++)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow.ItemArray = new object[] { Guid.NewGuid().ToString("N"), "张三", 7*(3+i), i>1?"安徽":"湖北", DateTime.Now.AddDays(4) };
                dataTable.Rows.Add(dataRow);
            }
            this.dataGridView1.DataSource = dataTable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable1 = this.dataGridView1.DataSource as DataTable;
                DataRow dataRow = dataTable1.NewRow();
                dataRow.SetField<string>("Id", Guid.NewGuid().ToString("N"));
                DetailForm formDetail = new DetailForm(dataRow);
                formDetail.Owner = this;
                formDetail.StartPosition = FormStartPosition.CenterParent;
                formDetail.ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        
        /// <summary>
        /// 修改第一行
        /// </summary>
        private void btn2_Click(object sender, EventArgs e)
        {
            if (dataTable.Rows.Count > 0)
            {
                DataRow dataRow = dataTable.Rows[0];
                int v = new Random().Next(1, 20);
                dataRow.ItemArray = new object[] { Guid.NewGuid().ToString("N"), "人名" + v, 29 + v, "浙江", DateTime.Now.AddDays(2) };
            }
            
        }

        /// <summary>
        /// 删除第一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if(dataTable.Rows.Count > 0)
            {
                dataTable.Rows.RemoveAt(0);
            }
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int columnIndex = e.ColumnIndex;
                int rowIndex = e.RowIndex;
                
                if (columnIndex + rowIndex <= 0)
                {
                    return;
                }

                DataGridViewColumn dataGridViewColumn = this.dataGridView1.Columns[columnIndex];
                string name = dataGridViewColumn.Name;

                if ("detail_row".Equals(name))
                {
                    DataGridView dataGridView = sender as DataGridView;
                    DataTable dataTable1 = dataGridView.DataSource as DataTable;
                    DataRow dataRow = dataTable1.Rows[rowIndex];
                    DetailForm formDetail = new DetailForm(dataRow);
                    formDetail.Owner = this;
                    formDetail.StartPosition = FormStartPosition.CenterParent;
                    formDetail.ShowDialog();
                }
       
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
